

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="Description" content="scikit-learn: machine learning in Python">

  
  <title>Utilities for Developers &mdash; scikit-learn 0.22 documentation</title>
  
  <link rel="canonical" href="http://scikit-learn.org/stable/developers/utilities.html" />

  
  <link rel="shortcut icon" href="../_static/favicon.ico"/>
  

  <link rel="stylesheet" href="../_static/css/vendor/bootstrap.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/gallery.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script> 
</head>
<body>
<nav id="navbar" class="sk-docs-navbar navbar navbar-expand-md navbar-light bg-light py-0">
  <div class="container-fluid sk-docs-container px-0">
      <a class="navbar-brand py-0" href="../index.html">
        <img
          class="sk-brand-img"
          src="../_static/scikit-learn-logo-small.png"
          alt="logo"/>
      </a>
    <button
      id="sk-navbar-toggler"
      class="navbar-toggler"
      type="button"
      data-toggle="collapse"
      data-target="#navbarSupportedContent"
      aria-controls="navbarSupportedContent"
      aria-expanded="false"
      aria-label="Toggle navigation"
    >
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="sk-navbar-collapse collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item">
          <a class="sk-nav-link nav-link" href="../install.html">Install</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link" href="../user_guide.html">User Guide</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link" href="../modules/classes.html">API</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link" href="../auto_examples/index.html">Examples</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../getting_started.html">Getting Started</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../tutorial/index.html">Tutorial</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../glossary.html">Glossary</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="index.html">Development</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../faq.html">FAQ</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../related_projects.html">Related packages</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../roadmap.html">Roadmap</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../about.html">About us</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="https://github.com/scikit-learn/scikit-learn">GitHub</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="https://scikit-learn.org/dev/versions.html">Other Versions</a>
        </li>
        <li class="nav-item dropdown nav-more-item-dropdown">
          <a class="sk-nav-link nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">More</a>
          <div class="dropdown-menu" aria-labelledby="navbarDropdown">
              <a class="sk-nav-dropdown-item dropdown-item" href="../getting_started.html">Getting Started</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../tutorial/index.html">Tutorial</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../glossary.html">Glossary</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="index.html">Development</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../faq.html">FAQ</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../related_projects.html">Related packages</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../roadmap.html">Roadmap</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../about.html">About us</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="https://github.com/scikit-learn/scikit-learn">GitHub</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="https://scikit-learn.org/dev/versions.html">Other Versions</a>
          </div>
        </li>
      </ul>
      <div id="searchbox" role="search">
          <div class="searchformwrapper">
          <form class="search" action="../search.html" method="get">
            <input class="sk-search-text-input" type="text" name="q" aria-labelledby="searchlabel" />
            <input class="sk-search-text-btn" type="submit" value="Go" />
          </form>
          </div>
      </div>
    </div>
  </div>
</nav>
<div class="d-flex" id="sk-doc-wrapper">
    <input type="checkbox" name="sk-toggle-checkbox" id="sk-toggle-checkbox">
    <label id="sk-sidemenu-toggle" class="sk-btn-toggle-toc btn sk-btn-primary" for="sk-toggle-checkbox">Toggle Menu</label>
    <div id="sk-sidebar-wrapper" class="border-right">
      <div class="sk-sidebar-toc-wrapper">
        <div class="sk-sidebar-toc-logo">
          <a href="../index.html">
            <img
              class="sk-brand-img"
              src="../_static/scikit-learn-logo-small.png"
              alt="logo"/>
          </a>
        </div>
        <div class="btn-group w-100 mb-2" role="group" aria-label="rellinks">
            <a href="tips.html" role="button" class="btn sk-btn-rellink py-1" sk-rellink-tooltip="Developers’ Tips and Tricks">Prev</a><a href="index.html" role="button" class="btn sk-btn-rellink py-1" sk-rellink-tooltip="Developer’s Guide">Up</a>
            <a href="performance.html" role="button" class="btn sk-btn-rellink py-1" sk-rellink-tooltip="How to optimize for speed">Next</a>
        </div>
        <div class="alert alert-danger p-1 mb-2" role="alert">
          <p class="text-center mb-0">
          <strong>scikit-learn 0.22</strong><br/>
          <a href="http://scikit-learn.org/dev/versions.html">Other versions</a>
          </p>
        </div>
        <div class="alert alert-warning p-1 mb-2" role="alert">
          <p class="text-center mb-0">
            Please <a class="font-weight-bold" href="../about.html#citing-scikit-learn"><string>cite us</string></a> if you use the software.
          </p>
        </div>
          <div class="sk-sidebar-toc">
            <ul>
<li><a class="reference internal" href="#">Utilities for Developers</a><ul>
<li><a class="reference internal" href="#validation-tools">Validation Tools</a></li>
<li><a class="reference internal" href="#efficient-linear-algebra-array-operations">Efficient Linear Algebra &amp; Array Operations</a></li>
<li><a class="reference internal" href="#efficient-random-sampling">Efficient Random Sampling</a></li>
<li><a class="reference internal" href="#efficient-routines-for-sparse-matrices">Efficient Routines for Sparse Matrices</a></li>
<li><a class="reference internal" href="#graph-routines">Graph Routines</a></li>
<li><a class="reference internal" href="#testing-functions">Testing Functions</a></li>
<li><a class="reference internal" href="#multiclass-and-multilabel-utility-function">Multiclass and multilabel utility function</a></li>
<li><a class="reference internal" href="#helper-functions">Helper Functions</a></li>
<li><a class="reference internal" href="#hash-functions">Hash Functions</a></li>
<li><a class="reference internal" href="#warnings-and-exceptions">Warnings and Exceptions</a></li>
</ul>
</li>
</ul>

          </div>
      </div>
    </div>
    <div id="sk-page-content-wrapper">
      <div class="sk-page-content container-fluid body px-md-3" role="main">
        
  <div class="section" id="utilities-for-developers">
<span id="developers-utils"></span><h1>Utilities for Developers<a class="headerlink" href="#utilities-for-developers" title="Permalink to this headline">¶</a></h1>
<p>Scikit-learn contains a number of utilities to help with development.  These are
located in <a class="reference internal" href="../modules/classes.html#module-sklearn.utils" title="sklearn.utils"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sklearn.utils</span></code></a>, and include tools in a number of categories.
All the following functions and classes are in the module <a class="reference internal" href="../modules/classes.html#module-sklearn.utils" title="sklearn.utils"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sklearn.utils</span></code></a>.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>These utilities are meant to be used internally within the scikit-learn
package.  They are not guaranteed to be stable between versions of
scikit-learn.  Backports, in particular, will be removed as the scikit-learn
dependencies evolve.</p>
</div>
<div class="section" id="validation-tools">
<h2>Validation Tools<a class="headerlink" href="#validation-tools" title="Permalink to this headline">¶</a></h2>
<p>These are tools used to check and validate input.  When you write a function
which accepts arrays, matrices, or sparse matrices as arguments, the following
should be used when applicable.</p>
<ul class="simple">
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.assert_all_finite.html#sklearn.utils.assert_all_finite" title="sklearn.utils.assert_all_finite"><code class="xref py py-func docutils literal notranslate"><span class="pre">assert_all_finite</span></code></a>: Throw an error if array contains NaNs or Infs.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.as_float_array.html#sklearn.utils.as_float_array" title="sklearn.utils.as_float_array"><code class="xref py py-func docutils literal notranslate"><span class="pre">as_float_array</span></code></a>: convert input to an array of floats.  If a sparse
matrix is passed, a sparse matrix will be returned.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.check_array.html#sklearn.utils.check_array" title="sklearn.utils.check_array"><code class="xref py py-func docutils literal notranslate"><span class="pre">check_array</span></code></a>: check that input is a 2D array, raise error on sparse
matrices. Allowed sparse matrix formats can be given optionally, as well as
allowing 1D or N-dimensional arrays. Calls <a class="reference internal" href="../modules/generated/sklearn.utils.assert_all_finite.html#sklearn.utils.assert_all_finite" title="sklearn.utils.assert_all_finite"><code class="xref py py-func docutils literal notranslate"><span class="pre">assert_all_finite</span></code></a> by
default.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.check_X_y.html#sklearn.utils.check_X_y" title="sklearn.utils.check_X_y"><code class="xref py py-func docutils literal notranslate"><span class="pre">check_X_y</span></code></a>: check that X and y have consistent length, calls
check_array on X, and column_or_1d on y. For multilabel classification or
multitarget regression, specify multi_output=True, in which case check_array
will be called on y.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.indexable.html#sklearn.utils.indexable" title="sklearn.utils.indexable"><code class="xref py py-func docutils literal notranslate"><span class="pre">indexable</span></code></a>: check that all input arrays have consistent length and can
be sliced or indexed using safe_index.  This is used to validate input for
cross-validation.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.validation.check_memory.html#sklearn.utils.validation.check_memory" title="sklearn.utils.validation.check_memory"><code class="xref py py-func docutils literal notranslate"><span class="pre">validation.check_memory</span></code></a> checks that input is <code class="docutils literal notranslate"><span class="pre">joblib.Memory</span></code>-like,
which means that it can be converted into a
<code class="docutils literal notranslate"><span class="pre">sklearn.utils.Memory</span></code> instance (typically a str denoting
the <code class="docutils literal notranslate"><span class="pre">cachedir</span></code>) or has the same interface.</p></li>
</ul>
<p>If your code relies on a random number generator, it should never use
functions like <code class="docutils literal notranslate"><span class="pre">numpy.random.random</span></code> or <code class="docutils literal notranslate"><span class="pre">numpy.random.normal</span></code>.  This
approach can lead to repeatability issues in unit tests.  Instead, a
<code class="docutils literal notranslate"><span class="pre">numpy.random.RandomState</span></code> object should be used, which is built from
a <code class="docutils literal notranslate"><span class="pre">random_state</span></code> argument passed to the class or function.  The function
<a class="reference internal" href="../modules/generated/sklearn.utils.check_random_state.html#sklearn.utils.check_random_state" title="sklearn.utils.check_random_state"><code class="xref py py-func docutils literal notranslate"><span class="pre">check_random_state</span></code></a>, below, can then be used to create a random
number generator object.</p>
<ul class="simple">
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.check_random_state.html#sklearn.utils.check_random_state" title="sklearn.utils.check_random_state"><code class="xref py py-func docutils literal notranslate"><span class="pre">check_random_state</span></code></a>: create a <code class="docutils literal notranslate"><span class="pre">np.random.RandomState</span></code> object from
a parameter <code class="docutils literal notranslate"><span class="pre">random_state</span></code>.</p>
<ul>
<li><p>If <code class="docutils literal notranslate"><span class="pre">random_state</span></code> is <code class="docutils literal notranslate"><span class="pre">None</span></code> or <code class="docutils literal notranslate"><span class="pre">np.random</span></code>, then a
randomly-initialized <code class="docutils literal notranslate"><span class="pre">RandomState</span></code> object is returned.</p></li>
<li><p>If <code class="docutils literal notranslate"><span class="pre">random_state</span></code> is an integer, then it is used to seed a new
<code class="docutils literal notranslate"><span class="pre">RandomState</span></code> object.</p></li>
<li><p>If <code class="docutils literal notranslate"><span class="pre">random_state</span></code> is a <code class="docutils literal notranslate"><span class="pre">RandomState</span></code> object, then it is passed through.</p></li>
</ul>
</li>
</ul>
<p>For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn.utils</span> <span class="kn">import</span> <span class="n">check_random_state</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">random_state</span> <span class="o">=</span> <span class="mi">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">random_state</span> <span class="o">=</span> <span class="n">check_random_state</span><span class="p">(</span><span class="n">random_state</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">random_state</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
<span class="go">array([0.5488135 , 0.71518937, 0.60276338, 0.54488318])</span>
</pre></div>
</div>
<p>When developing your own scikit-learn compatible estimator, the following
helpers are available.</p>
<ul class="simple">
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.validation.check_is_fitted.html#sklearn.utils.validation.check_is_fitted" title="sklearn.utils.validation.check_is_fitted"><code class="xref py py-func docutils literal notranslate"><span class="pre">validation.check_is_fitted</span></code></a>: check that the estimator has been fitted
before calling <code class="docutils literal notranslate"><span class="pre">transform</span></code>, <code class="docutils literal notranslate"><span class="pre">predict</span></code>, or similar methods. This helper
allows to raise a standardized error message across estimator.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.validation.has_fit_parameter.html#sklearn.utils.validation.has_fit_parameter" title="sklearn.utils.validation.has_fit_parameter"><code class="xref py py-func docutils literal notranslate"><span class="pre">validation.has_fit_parameter</span></code></a>: check that a given parameter is
supported in the <code class="docutils literal notranslate"><span class="pre">fit</span></code> method of a given estimator.</p></li>
</ul>
</div>
<div class="section" id="efficient-linear-algebra-array-operations">
<h2>Efficient Linear Algebra &amp; Array Operations<a class="headerlink" href="#efficient-linear-algebra-array-operations" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.extmath.randomized_range_finder.html#sklearn.utils.extmath.randomized_range_finder" title="sklearn.utils.extmath.randomized_range_finder"><code class="xref py py-func docutils literal notranslate"><span class="pre">extmath.randomized_range_finder</span></code></a>: construct an orthonormal matrix
whose range approximates the range of the input.  This is used in
<a class="reference internal" href="../modules/generated/sklearn.utils.extmath.randomized_svd.html#sklearn.utils.extmath.randomized_svd" title="sklearn.utils.extmath.randomized_svd"><code class="xref py py-func docutils literal notranslate"><span class="pre">extmath.randomized_svd</span></code></a>, below.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.extmath.randomized_svd.html#sklearn.utils.extmath.randomized_svd" title="sklearn.utils.extmath.randomized_svd"><code class="xref py py-func docutils literal notranslate"><span class="pre">extmath.randomized_svd</span></code></a>: compute the k-truncated randomized SVD.
This algorithm finds the exact truncated singular values decomposition
using randomization to speed up the computations. It is particularly
fast on large matrices on which you wish to extract only a small
number of components.</p></li>
<li><p><code class="xref py py-func docutils literal notranslate"><span class="pre">arrayfuncs.cholesky_delete</span></code>:
(used in <a class="reference internal" href="../modules/generated/sklearn.linear_model.lars_path.html#sklearn.linear_model.lars_path" title="sklearn.linear_model.lars_path"><code class="xref py py-func docutils literal notranslate"><span class="pre">sklearn.linear_model.lars_path</span></code></a>)  Remove an
item from a cholesky factorization.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.arrayfuncs.min_pos.html#sklearn.utils.arrayfuncs.min_pos" title="sklearn.utils.arrayfuncs.min_pos"><code class="xref py py-func docutils literal notranslate"><span class="pre">arrayfuncs.min_pos</span></code></a>: (used in <code class="docutils literal notranslate"><span class="pre">sklearn.linear_model.least_angle</span></code>)
Find the minimum of the positive values within an array.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.extmath.fast_logdet.html#sklearn.utils.extmath.fast_logdet" title="sklearn.utils.extmath.fast_logdet"><code class="xref py py-func docutils literal notranslate"><span class="pre">extmath.fast_logdet</span></code></a>: efficiently compute the log of the determinant
of a matrix.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.extmath.density.html#sklearn.utils.extmath.density" title="sklearn.utils.extmath.density"><code class="xref py py-func docutils literal notranslate"><span class="pre">extmath.density</span></code></a>: efficiently compute the density of a sparse vector</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.extmath.safe_sparse_dot.html#sklearn.utils.extmath.safe_sparse_dot" title="sklearn.utils.extmath.safe_sparse_dot"><code class="xref py py-func docutils literal notranslate"><span class="pre">extmath.safe_sparse_dot</span></code></a>: dot product which will correctly handle
<code class="docutils literal notranslate"><span class="pre">scipy.sparse</span></code> inputs.  If the inputs are dense, it is equivalent to
<code class="docutils literal notranslate"><span class="pre">numpy.dot</span></code>.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.extmath.weighted_mode.html#sklearn.utils.extmath.weighted_mode" title="sklearn.utils.extmath.weighted_mode"><code class="xref py py-func docutils literal notranslate"><span class="pre">extmath.weighted_mode</span></code></a>: an extension of <code class="docutils literal notranslate"><span class="pre">scipy.stats.mode</span></code> which
allows each item to have a real-valued weight.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.resample.html#sklearn.utils.resample" title="sklearn.utils.resample"><code class="xref py py-func docutils literal notranslate"><span class="pre">resample</span></code></a>: Resample arrays or sparse matrices in a consistent way.
used in <a class="reference internal" href="../modules/generated/sklearn.utils.shuffle.html#sklearn.utils.shuffle" title="sklearn.utils.shuffle"><code class="xref py py-func docutils literal notranslate"><span class="pre">shuffle</span></code></a>, below.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.shuffle.html#sklearn.utils.shuffle" title="sklearn.utils.shuffle"><code class="xref py py-func docutils literal notranslate"><span class="pre">shuffle</span></code></a>: Shuffle arrays or sparse matrices in a consistent way.
Used in <a class="reference internal" href="../modules/generated/sklearn.cluster.k_means.html#sklearn.cluster.k_means" title="sklearn.cluster.k_means"><code class="xref py py-func docutils literal notranslate"><span class="pre">sklearn.cluster.k_means</span></code></a>.</p></li>
</ul>
</div>
<div class="section" id="efficient-random-sampling">
<h2>Efficient Random Sampling<a class="headerlink" href="#efficient-random-sampling" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.random.sample_without_replacement.html#sklearn.utils.random.sample_without_replacement" title="sklearn.utils.random.sample_without_replacement"><code class="xref py py-func docutils literal notranslate"><span class="pre">random.sample_without_replacement</span></code></a>: implements efficient algorithms
for sampling <code class="docutils literal notranslate"><span class="pre">n_samples</span></code> integers from a population of size <code class="docutils literal notranslate"><span class="pre">n_population</span></code>
without replacement.</p></li>
</ul>
</div>
<div class="section" id="efficient-routines-for-sparse-matrices">
<h2>Efficient Routines for Sparse Matrices<a class="headerlink" href="#efficient-routines-for-sparse-matrices" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">sklearn.utils.sparsefuncs</span></code> cython module hosts compiled extensions to
efficiently process <code class="docutils literal notranslate"><span class="pre">scipy.sparse</span></code> data.</p>
<ul class="simple">
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.sparsefuncs.mean_variance_axis.html#sklearn.utils.sparsefuncs.mean_variance_axis" title="sklearn.utils.sparsefuncs.mean_variance_axis"><code class="xref py py-func docutils literal notranslate"><span class="pre">sparsefuncs.mean_variance_axis</span></code></a>: compute the means and
variances along a specified axis of a CSR matrix.
Used for normalizing the tolerance stopping criterion in
<a class="reference internal" href="../modules/generated/sklearn.cluster.KMeans.html#sklearn.cluster.KMeans" title="sklearn.cluster.KMeans"><code class="xref py py-class docutils literal notranslate"><span class="pre">sklearn.cluster.KMeans</span></code></a>.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.sparsefuncs_fast.inplace_csr_row_normalize_l1.html#sklearn.utils.sparsefuncs_fast.inplace_csr_row_normalize_l1" title="sklearn.utils.sparsefuncs_fast.inplace_csr_row_normalize_l1"><code class="xref py py-func docutils literal notranslate"><span class="pre">sparsefuncs_fast.inplace_csr_row_normalize_l1</span></code></a> and
<a class="reference internal" href="../modules/generated/sklearn.utils.sparsefuncs_fast.inplace_csr_row_normalize_l2.html#sklearn.utils.sparsefuncs_fast.inplace_csr_row_normalize_l2" title="sklearn.utils.sparsefuncs_fast.inplace_csr_row_normalize_l2"><code class="xref py py-func docutils literal notranslate"><span class="pre">sparsefuncs_fast.inplace_csr_row_normalize_l2</span></code></a>: can be used to normalize
individual sparse samples to unit L1 or L2 norm as done in
<a class="reference internal" href="../modules/generated/sklearn.preprocessing.Normalizer.html#sklearn.preprocessing.Normalizer" title="sklearn.preprocessing.Normalizer"><code class="xref py py-class docutils literal notranslate"><span class="pre">sklearn.preprocessing.Normalizer</span></code></a>.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.sparsefuncs.inplace_csr_column_scale.html#sklearn.utils.sparsefuncs.inplace_csr_column_scale" title="sklearn.utils.sparsefuncs.inplace_csr_column_scale"><code class="xref py py-func docutils literal notranslate"><span class="pre">sparsefuncs.inplace_csr_column_scale</span></code></a>: can be used to multiply the
columns of a CSR matrix by a constant scale (one scale per column).
Used for scaling features to unit standard deviation in
<a class="reference internal" href="../modules/generated/sklearn.preprocessing.StandardScaler.html#sklearn.preprocessing.StandardScaler" title="sklearn.preprocessing.StandardScaler"><code class="xref py py-class docutils literal notranslate"><span class="pre">sklearn.preprocessing.StandardScaler</span></code></a>.</p></li>
</ul>
</div>
<div class="section" id="graph-routines">
<h2>Graph Routines<a class="headerlink" href="#graph-routines" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.graph.single_source_shortest_path_length.html#sklearn.utils.graph.single_source_shortest_path_length" title="sklearn.utils.graph.single_source_shortest_path_length"><code class="xref py py-func docutils literal notranslate"><span class="pre">graph.single_source_shortest_path_length</span></code></a>:
(not currently used in scikit-learn)
Return the shortest path from a single source
to all connected nodes on a graph.  Code is adapted from <a class="reference external" href="https://networkx.github.io/">networkx</a>.
If this is ever needed again, it would be far faster to use a single
iteration of Dijkstra’s algorithm from <code class="docutils literal notranslate"><span class="pre">graph_shortest_path</span></code>.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.graph_shortest_path.graph_shortest_path.html#sklearn.utils.graph_shortest_path.graph_shortest_path" title="sklearn.utils.graph_shortest_path.graph_shortest_path"><code class="xref py py-func docutils literal notranslate"><span class="pre">graph_shortest_path.graph_shortest_path</span></code></a>:
(used in <a class="reference internal" href="../modules/generated/sklearn.manifold.Isomap.html#sklearn.manifold.Isomap" title="sklearn.manifold.Isomap"><code class="xref py py-class docutils literal notranslate"><span class="pre">sklearn.manifold.Isomap</span></code></a>)
Return the shortest path between all pairs of connected points on a directed
or undirected graph.  Both the Floyd-Warshall algorithm and Dijkstra’s
algorithm are available.  The algorithm is most efficient when the
connectivity matrix is a <code class="docutils literal notranslate"><span class="pre">scipy.sparse.csr_matrix</span></code>.</p></li>
</ul>
</div>
<div class="section" id="testing-functions">
<h2>Testing Functions<a class="headerlink" href="#testing-functions" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.all_estimators.html#sklearn.utils.all_estimators" title="sklearn.utils.all_estimators"><code class="xref py py-func docutils literal notranslate"><span class="pre">all_estimators</span></code></a> : returns a list of all estimators in
scikit-learn to test for consistent behavior and interfaces.</p></li>
</ul>
</div>
<div class="section" id="multiclass-and-multilabel-utility-function">
<h2>Multiclass and multilabel utility function<a class="headerlink" href="#multiclass-and-multilabel-utility-function" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.multiclass.is_multilabel.html#sklearn.utils.multiclass.is_multilabel" title="sklearn.utils.multiclass.is_multilabel"><code class="xref py py-func docutils literal notranslate"><span class="pre">multiclass.is_multilabel</span></code></a>: Helper function to check if the task
is a multi-label classification one.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.multiclass.unique_labels.html#sklearn.utils.multiclass.unique_labels" title="sklearn.utils.multiclass.unique_labels"><code class="xref py py-func docutils literal notranslate"><span class="pre">multiclass.unique_labels</span></code></a>: Helper function to extract an ordered
array of unique labels from different formats of target.</p></li>
</ul>
</div>
<div class="section" id="helper-functions">
<h2>Helper Functions<a class="headerlink" href="#helper-functions" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.gen_even_slices.html#sklearn.utils.gen_even_slices" title="sklearn.utils.gen_even_slices"><code class="xref py py-class docutils literal notranslate"><span class="pre">gen_even_slices</span></code></a>: generator to create <code class="docutils literal notranslate"><span class="pre">n</span></code>-packs of slices going up
to <code class="docutils literal notranslate"><span class="pre">n</span></code>.  Used in <a class="reference internal" href="../modules/generated/sklearn.decomposition.dict_learning.html#sklearn.decomposition.dict_learning" title="sklearn.decomposition.dict_learning"><code class="xref py py-func docutils literal notranslate"><span class="pre">sklearn.decomposition.dict_learning</span></code></a> and
<a class="reference internal" href="../modules/generated/sklearn.cluster.k_means.html#sklearn.cluster.k_means" title="sklearn.cluster.k_means"><code class="xref py py-func docutils literal notranslate"><span class="pre">sklearn.cluster.k_means</span></code></a>.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.safe_mask.html#sklearn.utils.safe_mask" title="sklearn.utils.safe_mask"><code class="xref py py-func docutils literal notranslate"><span class="pre">safe_mask</span></code></a>: Helper function to convert a mask to the format expected
by the numpy array or scipy sparse matrix on which to use it (sparse
matrices support integer indices only while numpy arrays support both
boolean masks and integer indices).</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.safe_sqr.html#sklearn.utils.safe_sqr" title="sklearn.utils.safe_sqr"><code class="xref py py-func docutils literal notranslate"><span class="pre">safe_sqr</span></code></a>: Helper function for unified squaring (<code class="docutils literal notranslate"><span class="pre">**2</span></code>) of
array-likes, matrices and sparse matrices.</p></li>
</ul>
</div>
<div class="section" id="hash-functions">
<h2>Hash Functions<a class="headerlink" href="#hash-functions" title="Permalink to this headline">¶</a></h2>
<ul>
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.murmurhash3_32.html#sklearn.utils.murmurhash3_32" title="sklearn.utils.murmurhash3_32"><code class="xref py py-func docutils literal notranslate"><span class="pre">murmurhash3_32</span></code></a> provides a python wrapper for the
<code class="docutils literal notranslate"><span class="pre">MurmurHash3_x86_32</span></code> C++ non cryptographic hash function. This hash
function is suitable for implementing lookup tables, Bloom filters,
Count Min Sketch, feature hashing and implicitly defined sparse
random projections:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn.utils</span> <span class="kn">import</span> <span class="n">murmurhash3_32</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">murmurhash3_32</span><span class="p">(</span><span class="s2">&quot;some feature&quot;</span><span class="p">,</span> <span class="n">seed</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="mi">384616559</span>
<span class="go">True</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">murmurhash3_32</span><span class="p">(</span><span class="s2">&quot;some feature&quot;</span><span class="p">,</span> <span class="n">seed</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3910350737</span>
<span class="go">True</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">sklearn.utils.murmurhash</span></code> module can also be “cimported” from
other cython modules so as to benefit from the high performance of
MurmurHash while skipping the overhead of the Python interpreter.</p>
</li>
</ul>
</div>
<div class="section" id="warnings-and-exceptions">
<h2>Warnings and Exceptions<a class="headerlink" href="#warnings-and-exceptions" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><p><a class="reference internal" href="../modules/generated/sklearn.utils.deprecated.html#sklearn.utils.deprecated" title="sklearn.utils.deprecated"><code class="xref py py-class docutils literal notranslate"><span class="pre">deprecated</span></code></a>: Decorator to mark a function or class as deprecated.</p></li>
<li><p><a class="reference internal" href="../modules/generated/sklearn.exceptions.ConvergenceWarning.html#sklearn.exceptions.ConvergenceWarning" title="sklearn.exceptions.ConvergenceWarning"><code class="xref py py-class docutils literal notranslate"><span class="pre">sklearn.exceptions.ConvergenceWarning</span></code></a>: Custom warning to catch
convergence problems. Used in <code class="docutils literal notranslate"><span class="pre">sklearn.covariance.graphical_lasso</span></code>.</p></li>
</ul>
</div>
</div>


      </div>
    <div class="container">
      <footer class="sk-content-footer">
            &copy; 2007 - 2019, scikit-learn developers (BSD License).
          <a href="../_sources/developers/utilities.rst.txt" rel="nofollow">Show this page source</a>
      </footer>
    </div>
  </div>
</div>
<script src="../_static/js/vendor/bootstrap.min.js"></script>

<script>
    window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
    ga('create', 'UA-22606712-2', 'auto');
    ga('set', 'anonymizeIp', true);
    ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>


<script>
$(document).ready(function() {
    /* Add a [>>>] button on the top-right corner of code samples to hide
     * the >>> and ... prompts and the output and thus make the code
     * copyable. */
    var div = $('.highlight-python .highlight,' +
                '.highlight-python3 .highlight,' +
                '.highlight-pycon .highlight,' +
		'.highlight-default .highlight')
    var pre = div.find('pre');

    // get the styles from the current theme
    pre.parent().parent().css('position', 'relative');
    var hide_text = 'Hide prompts and outputs';
    var show_text = 'Show prompts and outputs';

    // create and add the button to all the code blocks that contain >>>
    div.each(function(index) {
        var jthis = $(this);
        if (jthis.find('.gp').length > 0) {
            var button = $('<span class="copybutton">&gt;&gt;&gt;</span>');
            button.attr('title', hide_text);
            button.data('hidden', 'false');
            jthis.prepend(button);
        }
        // tracebacks (.gt) contain bare text elements that need to be
        // wrapped in a span to work with .nextUntil() (see later)
        jthis.find('pre:has(.gt)').contents().filter(function() {
            return ((this.nodeType == 3) && (this.data.trim().length > 0));
        }).wrap('<span>');
    });

    // define the behavior of the button when it's clicked
    $('.copybutton').click(function(e){
        e.preventDefault();
        var button = $(this);
        if (button.data('hidden') === 'false') {
            // hide the code output
            button.parent().find('.go, .gp, .gt').hide();
            button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'hidden');
            button.css('text-decoration', 'line-through');
            button.attr('title', show_text);
            button.data('hidden', 'true');
        } else {
            // show the code output
            button.parent().find('.go, .gp, .gt').show();
            button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'visible');
            button.css('text-decoration', 'none');
            button.attr('title', hide_text);
            button.data('hidden', 'false');
        }
    });

	/*** Add permalink buttons next to glossary terms ***/
	$('dl.glossary > dt[id]').append(function() {
		return ('<a class="headerlink" href="#' +
			    this.getAttribute('id') +
			    '" title="Permalink to this term">¶</a>');
	});
  /*** Hide navbar when scrolling down ***/
  // Returns true when headerlink target matches hash in url
  (function() {
    hashTargetOnTop = function() {
        var hash = window.location.hash;
        if ( hash.length < 2 ) { return false; }

        var target = document.getElementById( hash.slice(1) );
        if ( target === null ) { return false; }

        var top = target.getBoundingClientRect().top;
        return (top < 2) && (top > -2);
    };

    // Hide navbar on load if hash target is on top
    var navBar = document.getElementById("navbar");
    var navBarToggler = document.getElementById("sk-navbar-toggler");
    var navBarHeightHidden = "-" + navBar.getBoundingClientRect().height + "px";
    var $window = $(window);

    hideNavBar = function() {
        navBar.style.top = navBarHeightHidden;
    };

    showNavBar = function() {
        navBar.style.top = "0";
    }

    if (hashTargetOnTop()) {
        hideNavBar()
    }

    var prevScrollpos = window.pageYOffset;
    hideOnScroll = function(lastScrollTop) {
        if (($window.width() < 768) && (navBarToggler.getAttribute("aria-expanded") === 'true')) {
            return;
        }
        if (lastScrollTop > 2 && (prevScrollpos <= lastScrollTop) || hashTargetOnTop()){
            hideNavBar()
        } else {
            showNavBar()
        }
        prevScrollpos = lastScrollTop;
    };

    /*** high preformance scroll event listener***/
    var raf = window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        window.oRequestAnimationFrame;
    var lastScrollTop = $window.scrollTop();

    if (raf) {
        loop();
    }

    function loop() {
        var scrollTop = $window.scrollTop();
        if (lastScrollTop === scrollTop) {
            raf(loop);
            return;
        } else {
            lastScrollTop = scrollTop;
            hideOnScroll(lastScrollTop);
            raf(loop);
        }
    }
  })();
});

</script>
    
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
    
</body>
</html>